package cn.schoolwow.workflow.test.module.gateway.testcase;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.dto.FlowContextDataRange;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.QuickWorkFlow;
import cn.schoolwow.workflow.domain.instance.StartWorkFlowRequest;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceStatus;
import cn.schoolwow.workflow.domain.task.TaskType;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskQuery;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskResponse;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import cn.schoolwow.workflow.test.module.analyzeWorkReport.audit.single.common.CheckWorkFlowResult;
import org.junit.Assert;

import java.util.Arrays;
import java.util.List;

public class StartResignMultipleBranchAuditTestCaseFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        startWorkFlow(flowContext);
        directLeaderAudit(flowContext);
        multipleBranchAudit(flowContext);
        flowContext.startFlow(new CheckWorkFlowResult())
                .putInstanceData(WorkFlowInstanceStatus.Finished)
                .execute();
    }

    @Override
    public String name() {
        return "离职多分支并行审批工作流测试用例";
    }

    private void startWorkFlow(FlowContext flowContext){
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        StartWorkFlowRequest startWorkFlowRequest = new StartWorkFlowRequest();
        startWorkFlowRequest.definitionName = "员工离职多流程并行网关审批";
        startWorkFlowRequest.initiator = "张三";
        flowContext.putInstanceData(startWorkFlowRequest, FlowContextDataRange.ContextThreadLocal);

        WorkFlowInstance workFlowInstance = quickWorkFlow.startWorkFlow(startWorkFlowRequest);
        flowContext.putInstanceData(workFlowInstance);
    }

    private void directLeaderAudit(FlowContext flowContext){
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();
        workFlowTaskQuery.assignerList = Arrays.asList("直属领导");
        WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
        quickWorkFlow.completeTask(workFlowTaskResponse.taskId, TaskType.COMPLETE);
    }

    private void multipleBranchAudit(FlowContext flowContext){
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();
        workFlowTaskQuery.assignerList.add("人事");
        workFlowTaskQuery.assignerList.add("财务");
        workFlowTaskQuery.assignerList.add("运维");
        List<WorkFlowTaskResponse> workFlowTaskResponseList = quickWorkFlow.getWorkFlowTaskList(workFlowTaskQuery);
        Assert.assertEquals(workFlowTaskQuery.assignerList.size(), workFlowTaskResponseList.size());
        for(WorkFlowTaskResponse workFlowTaskResponse:workFlowTaskResponseList){
            quickWorkFlow.completeTask(workFlowTaskResponse.taskId, TaskType.COMPLETE);
        }
    }
}
